﻿using HardcoreGame.Content.Buffs;
using HardcoreGame.Content.Projectiles.Javelin;
using Terraria;
using Terraria.ID;
using Terraria.ModLoader;

namespace HardcoreGame.Common.GlobalNPCs
{
    public class JavelinNPC : GlobalNPC
    {
        public override bool InstancePerEntity => true;
        public bool CopperJavelin = false;
        public bool TinJavelin = false;
        public bool IronJavelin = false;
        public bool LeadJavelin = false;
        public bool TungstenJavelin = false;
        public bool GoldJavelin = false;
        public bool PlatinumJavelin = false;
        public bool SilverJavelin = false;
        public override void ResetEffects(NPC npc)
        {
            CopperJavelin = false;
            TinJavelin = false;
            IronJavelin = false;
            LeadJavelin = false;
            TungstenJavelin = false;
            GoldJavelin = false;
            PlatinumJavelin = false;
            SilverJavelin = false;
        }
        public override void SetDefaults(NPC entity)
        {
            entity.buffImmune[ModContent.BuffType<CopperJavelin>()] = entity.buffImmune[BuffID.BoneJavelin];
            entity.buffImmune[ModContent.BuffType<TinJavelin>()] = entity.buffImmune[BuffID.BoneJavelin];
            entity.buffImmune[ModContent.BuffType<IronJavelin>()] = entity.buffImmune[BuffID.BoneJavelin];
            entity.buffImmune[ModContent.BuffType<LeadJavelin>()] = entity.buffImmune[BuffID.BoneJavelin];
            entity.buffImmune[ModContent.BuffType<TungstenJavelin>()] = entity.buffImmune[BuffID.BoneJavelin];
            entity.buffImmune[ModContent.BuffType<GoldJavelin>()] = entity.buffImmune[BuffID.BoneJavelin];
            entity.buffImmune[ModContent.BuffType<PlatinumJavelin>()] = entity.buffImmune[BuffID.BoneJavelin];
            entity.buffImmune[ModContent.BuffType<SilverJavelin>()] = entity.buffImmune[BuffID.BoneJavelin];
        }
        public override void UpdateLifeRegen(NPC npc, ref int damage)
        {
            if (CopperJavelin)
            {
                if (npc.lifeRegen > 0)
                {
                    npc.lifeRegen = 0;
                }
                int JavelinCount = 0;
                for (int i = 0; i < 1000; i++)
                {
                    Projectile p = Main.projectile[i];
                    if (p.active && p.type == ModContent.ProjectileType<CopperJavelinProj>() && p.ai[0] == 1f && p.ai[1] == npc.whoAmI)
                    {
                        JavelinCount++;
                    }
                }
                npc.lifeRegen -= JavelinCount * 2;
                if (damage < JavelinCount)
                {
                    damage = JavelinCount;
                }
            }
            if (TinJavelin)
            {
                if (npc.lifeRegen > 0)
                {
                    npc.lifeRegen = 0;
                }
                int JavelinCount = 0;
                for (int i = 0; i < 1000; i++)
                {
                    Projectile p = Main.projectile[i];
                    if (p.active && p.type == ModContent.ProjectileType<TinJavelinProj>() && p.ai[0] == 1f && p.ai[1] == npc.whoAmI)
                    {
                        JavelinCount++;
                    }
                }
                npc.lifeRegen -= JavelinCount * 2;
                if (damage < JavelinCount)
                {
                    damage = JavelinCount;
                }
            }
            if (IronJavelin)
            {
                if (npc.lifeRegen > 0)
                {
                    npc.lifeRegen = 0;
                }
                int JavelinCount = 0;
                for (int i = 0; i < 1000; i++)
                {
                    Projectile p = Main.projectile[i];
                    if (p.active && p.type == ModContent.ProjectileType<IronJavelinProj>() && p.ai[0] == 1f && p.ai[1] == npc.whoAmI)
                    {
                        JavelinCount++;
                    }
                }
                npc.lifeRegen -= JavelinCount * 3;
                if (damage < JavelinCount * 2)
                {
                    damage = JavelinCount * 2;
                }
            }
            if (LeadJavelin)
            {
                if (npc.lifeRegen > 0)
                {
                    npc.lifeRegen = 0;
                }
                int JavelinCount = 0;
                for (int i = 0; i < 1000; i++)
                {
                    Projectile p = Main.projectile[i];
                    if (p.active && p.type == ModContent.ProjectileType<LeadJavelinProj>() && p.ai[0] == 1f && p.ai[1] == npc.whoAmI)
                    {
                        JavelinCount++;
                    }
                }
                npc.lifeRegen -= JavelinCount * 3;
                if (damage < JavelinCount * 2)
                {
                    damage = JavelinCount * 2;
                }
            }
            if (TungstenJavelin)
            {
                if (npc.lifeRegen > 0)
                {
                    npc.lifeRegen = 0;
                }
                int JavelinCount = 0;
                for (int i = 0; i < 1000; i++)
                {
                    Projectile p = Main.projectile[i];
                    if (p.active && p.type == ModContent.ProjectileType<TungstenJavelinProj>() && p.ai[0] == 1f && p.ai[1] == npc.whoAmI)
                    {
                        JavelinCount++;
                    }
                }
                npc.lifeRegen -= JavelinCount * 4;
                if (damage < JavelinCount * 3)
                {
                    damage = JavelinCount * 3;
                }
            }
            if (GoldJavelin)
            {
                if (npc.lifeRegen > 0)
                {
                    npc.lifeRegen = 0;
                }
                int JavelinCount = 0;
                for (int i = 0; i < 1000; i++)
                {
                    Projectile p = Main.projectile[i];
                    if (p.active && p.type == ModContent.ProjectileType<GoldJavelinProj>() && p.ai[0] == 1f && p.ai[1] == npc.whoAmI)
                    {
                        JavelinCount++;
                    }
                }
                npc.lifeRegen -= JavelinCount * 4;
                if (damage < JavelinCount * 3)
                {
                    damage = JavelinCount * 3;
                }
            }
            if (PlatinumJavelin)
            {
                if (npc.lifeRegen > 0)
                {
                    npc.lifeRegen = 0;
                }
                int JavelinCount = 0;
                for (int i = 0; i < 1000; i++)
                {
                    Projectile p = Main.projectile[i];
                    if (p.active && p.type == ModContent.ProjectileType<PlatinumJavelinProj>() && p.ai[0] == 1f && p.ai[1] == npc.whoAmI)
                    {
                        JavelinCount++;
                    }
                }
                npc.lifeRegen -= JavelinCount * 4;
                if (damage < JavelinCount * 3)
                {
                    damage = JavelinCount * 3;
                }
            }
            if (SilverJavelin)
            {
                if (npc.lifeRegen > 0)
                {
                    npc.lifeRegen = 0;
                }
                int JavelinCount = 0;
                for (int i = 0; i < 1000; i++)
                {
                    Projectile p = Main.projectile[i];
                    if (p.active && p.type == ModContent.ProjectileType<SilverJavelinProj>() && p.ai[0] == 1f && p.ai[1] == npc.whoAmI)
                    {
                        JavelinCount++;
                    }
                }
                npc.lifeRegen -= JavelinCount * 5;
                if (damage < JavelinCount * 4)
                {
                    damage = JavelinCount * 4;
                }
            }
        }
    }
}
